﻿using Tszy.Unlimited.Data.FreeSQL;

namespace Tszy.Examples.Database.FreeSQLs;

public class TransBlogService
{
    private readonly IFreeSQLRepository<FreeSQLTest.TestUser> _repository;
    private readonly IFreeSQLUnitOfWorkFactory _unitOfWorkFactory;

    public TransBlogService(IFreeSQLRepository<FreeSQLTest.TestUser> repository, IFreeSQLUnitOfWorkFactory unitOfWorkFactory)
    {
        _repository = repository;
        _unitOfWorkFactory = unitOfWorkFactory;

        Identity = Guid.NewGuid();
    }

    public Guid Identity { get; private set; }

    public async Task ExecuteAsync()
    {
        using var unitOfWork = _unitOfWorkFactory.Begin();

        var user = await _repository.Select.ForUpdate().Where(item => item.Id == 7).FirstAsync();

        user.Money += 10;

        await _repository.UpdateAsync(user);

        //await _repository.UpdateDiy.Set(item => item.Money + 1).Where(item => item.Id == 7).ExecuteAffrowsAsync();

        //unitOfWork.Rollback();
        unitOfWork.Commit();
    }
}